<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017/11/28
 * Time: 10:35
 */

namespace app\admin\model;


use think\Model;
use think\Db;

class Classroom extends Model
{
//    // 获取教师的使用状态
//    public function getStatusAttr($value){
//        $status = [
//            0 => '暂未使用',
//            1 => '正在使用'
//        ];
//        return $status[$value];
//    }



    /**
     * 查询所有教室信息记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['user.full_name|user.tel'] = array('like', "%{$keywords}%");
        }else{
            $where = [];
        }
        $classroom = $this
            ->alias('cla')
            ->join('user user', 'cla.classroom_owner=user.id', 'LEFT')
            ->join('classroom_type type','cla.classroom_type_id = type.id', 'LEFT')
            ->join('classroom_nature cn','cla.classroom_nature_id = cn.id', 'LEFT')
            ->join('user us','cla.rec_classroom_user = us.id', 'LEFT')
            ->join('area area','cla.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->field('cla.id,cla.classroom_sn,cla.classroom_address,type.classroom_type,cla.classroom_owner,
                cn.classroom_nature,cla.classroom_size,cla.classroom_condition_id, cla.classroom_acceptance_num,
                cla.classroom_start_time,cla.classroom_end_time,cla.classroom_environment_images,
                cla.classroom_out_images,user.tel as classroom_owner_tel,cla.classroom_in_images,
                user.full_name as classroom_owner_name,us.full_name as rec_classroom_user,
                us.tel as rec_tel,cla.area_code,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name,cla.status')
            ->where($where)
            ->order($order)
            ->select();
        if($classroom){
        foreach($classroom as $key =>$val){
            $classroom[$key] = $val->toArray();
            if(!empty($classroom[$key]['classroom_environment_images'])){
                    $classroom[$key]['classroom_environment_images'] = json_decode(htmlspecialchars_decode($classroom[$key]['classroom_environment_images']));
                }
                if(!empty($classroom[$key]['classroom_out_images'])){
                    $classroom[$key]['classroom_out_images'] = json_decode(htmlspecialchars_decode($classroom[$key]['classroom_out_images']));
                }
                if(!empty($classroom[$key]['classroom_in_images'])){
                    $classroom[$key]['classroom_in_images'] = json_decode(htmlspecialchars_decode($classroom[$key]['classroom_in_images']));
                }
            }
        }
        return $classroom;
    }

    /**
     * 查询所有教室信息记录总数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['user.full_name|user.tel'] = array('like', "%{$keywords}%");
        }else{
            $where = [];
        }
        $count = $this
            ->alias('cla')
            ->join('user user', 'cla.classroom_owner=user.id', 'LEFT')
            ->join('classroom_type type','cla.classroom_type_id = type.id', 'LEFT')
            ->join('classroom_nature cn','cla.classroom_nature_id = cn.id', 'LEFT')
            ->join('user us','cla.rec_classroom_user = us.id', 'LEFT')
            ->join('area area','cla.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->field('cla.id,cla.classroom_sn,cla.classroom_address,type.classroom_type,cla.classroom_owner,
                    cn.classroom_nature,cla.classroom_size,cla.classroom_condition_id, cla.classroom_acceptance_num,
                    cla.classroom_start_time,cla.classroom_end_time,cla.classroom_environment_images,
                    cla.classroom_out_images,user.tel as classroom_owner_tel,cla.classroom_in_images,
                    user.full_name as classroom_owner_name,us.full_name as rec_classroom_user,
                    us.tel as rec_tel,cla.area_code,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name,cla.status')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 查询单条记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=[],$order=""){
        $info = $this
            ->alias('cla')
            ->join('user user', 'cla.classroom_owner=user.id', 'LEFT')
            ->join('classroom_type type','cla.classroom_type_id = type.id', 'LEFT')
            ->join('classroom_nature cn','cla.classroom_nature_id = cn.id', 'LEFT')
            ->join('user us','cla.rec_classroom_user = us.id', 'LEFT')
            ->join('area area','cla.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->field('cla.id,cla.classroom_sn,cla.classroom_address,cla.classroom_condition_id,type.classroom_type,
                    cn.classroom_nature,cla.classroom_size, cla.classroom_acceptance_num,cla.classroom_owner,
                    cla.classroom_start_time,cla.classroom_end_time,cla.classroom_environment_images,
                    cla.classroom_out_images,user.tel as classroom_owner_tel,cla.classroom_in_images,
                    user.full_name as classroom_owner_name,us.full_name as rec_classroom_user,
                    us.tel as rec_tel,cla.area_code,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name,cla.lesson_id,cla.status')
            ->where($where)
            ->order($order)
            ->find();
        if($info){
            $info = $info ->toArray();
            if(!empty($info['classroom_environment_images'])){
                $info['classroom_environment_images'] = json_decode(htmlspecialchars_decode($info['classroom_environment_images']));
            }
            if(!empty($info['classroom_out_images'])){
                $info['classroom_out_images'] = json_decode(htmlspecialchars_decode($info['classroom_out_images']));
            }
            if(!empty($info['classroom_in_images'])){
                $info['classroom_in_images'] = json_decode(htmlspecialchars_decode($info['classroom_in_images']));
            }
            if(!empty($info['lesson_id'])){
                $lesson_name = Db::name('lesson')->where('id','in',$info['lesson_id'])->column("lesson_name");
                $info['lesson_name'] = implode($lesson_name,'，');
            }
        }
        return $info;
    }


    /**
     * 查询单条记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getClassroomById($id){
        $info = $this
            ->alias('cla')
            ->join('user user', 'cla.classroom_owner=user.id', 'LEFT')
            ->join('classroom_type type','cla.classroom_type_id = type.id', 'LEFT')
            ->join('classroom_nature cn','cla.classroom_nature_id = cn.id', 'LEFT')
            ->join('user us','cla.rec_classroom_user = us.id', 'LEFT')
            ->join('area area','cla.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->field('cla.id,cla.classroom_sn,cla.classroom_address,cla.classroom_condition_id,type.classroom_type,
                    cn.classroom_nature,cla.classroom_size, cla.classroom_acceptance_num,cla.classroom_owner,
                    cla.classroom_start_time,cla.classroom_end_time,cla.classroom_environment_images,
                    cla.classroom_out_images,user.tel as classroom_owner_tel,cla.classroom_in_images,
                    user.full_name as classroom_owner_name,us.full_name as rec_classroom_user,
                    us.tel as rec_tel,cla.area_code,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name,cla.classroom_address,cla.lesson_id,cla.status')
            ->where('cla.id='.$id)
            ->find();
        if($info){
            $info = $info ->toArray();

            $conditions = explode(',',$info['classroom_condition_id']);
            $classroom_conditions = "";
            foreach($conditions as $k=>$v){
                $furniture = Db::name('classroom_condition')->where('id',$v)->find();
                $classroom_conditions .= $furniture['classroom_condition'].'、';
            }
            $info['classroom_condition_id'] = trim($classroom_conditions,'、');

            if(!empty($info['classroom_environment_images'])){
                $info['classroom_environment_images'] = json_decode(htmlspecialchars_decode($info['classroom_environment_images']));
            }
            if(!empty($info['classroom_out_images'])){
                $info['classroom_out_images'] = json_decode(htmlspecialchars_decode($info['classroom_out_images']));
            }
            if(!empty($info['classroom_in_images'])){
                $info['classroom_in_images'] = json_decode(htmlspecialchars_decode($info['classroom_in_images']));
            }
            if(!empty($info['lesson_id'])){
                $lesson_name = Db::name('lesson')->where('id','in',$info['lesson_id'])->column("lesson_name");
                $info['lesson_name'] = implode($lesson_name,'，');
            }
        }
        return $info;
    }
}